"""
测试用例模块数据模型定义

作者: heruopu
© 2025 Heruopu. All rights reserved.
"""

from django.db import models
from projects.models import Version

# 用例设计方法选择
TEST_METHODS = (
    ('equivalence', '等价类'),
    ('boundary', '边界值'),
    ('decision_table', '判定表'),
    ('state_transition', '状态迁移'),
    ('use_case', '用例设计'),
    ('error_guessing', '错误推测'),
    ('scenario', '场景'),
    ('cause_effect', '因果图'),
    ('orthogonal', '正交法'),
    ('flowchart', '流程图法'),
    ('usability', '可用性测试'),
    ('functional', '功能测试'),
    ('security', '安全性测试'),
    ('compatibility', '兼容性测试'),
    ('other', '其他'),
)

# 用例状态选择
CASE_STATUSES = (
    ('draft', '草稿'),
    ('reviewing', '评审中'),
    ('reviewed', '已评审'),
    ('rejected', '已驳回'),
    ('obsolete', '已废弃'),
)

# 优先级选择
PRIORITIES = (
    ('blocker', '阻塞'),
    ('critical', '严重'),
    ('high', '高'),
    ('medium', '中'),
    ('low', '低'),
)

class TestCase(models.Model):
    """测试用例模型"""
    version = models.ForeignKey(Version, on_delete=models.CASCADE, related_name='test_cases', verbose_name='所属版本')
    case_id = models.CharField(max_length=100, verbose_name='用例ID', unique=True)
    title = models.CharField(max_length=500, verbose_name='用例标题')
    description = models.TextField(blank=True, null=True, verbose_name='用例描述')
    precondition = models.TextField(blank=True, null=True, verbose_name='前置条件')
    steps = models.TextField(verbose_name='测试步骤')
    expected_result = models.TextField(verbose_name='预期结果')
    actual_result = models.TextField(blank=True, null=True, verbose_name='实际结果')
    priority = models.CharField(max_length=50, choices=PRIORITIES, default='medium', verbose_name='优先级')
    status = models.CharField(max_length=50, choices=CASE_STATUSES, default='draft', verbose_name='状态')
    test_method = models.CharField(max_length=100, choices=TEST_METHODS, default='other', verbose_name='用例设计方法')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    created_by = models.CharField(max_length=100, null=True, blank=True, verbose_name='创建人')
    reviewed_by = models.CharField(max_length=100, null=True, blank=True, verbose_name='评审人')
    reviewed_at = models.DateTimeField(null=True, blank=True, verbose_name='评审时间')
    is_automated = models.BooleanField(default=False, verbose_name='是否自动化')
    automation_script = models.TextField(blank=True, null=True, verbose_name='自动化脚本')
    module = models.CharField(max_length=200, blank=True, null=True, verbose_name='所属模块')
    requirement_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='需求ID')

    def __str__(self):
        return f'{self.case_id}: {self.title}'

    class Meta:
        verbose_name = '测试用例'
        verbose_name_plural = '测试用例管理'
        ordering = ['case_id', '-created_at']
        indexes = [
            models.Index(fields=['version', 'status', 'priority']),
            models.Index(fields=['case_id']),
            models.Index(fields=['module']),
        ]
